import os
import traceback

import pypinyin
import random
import time

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.ui import WebDriverWait
from my_json import save_json

xpath = "/html/body/div[7]/div[1]/div[3]/ul/li[1]/div[1]/div[1]"
tem = "℃"
basic_url = "https://lishi.tianqi.com/{p}/{y}{m}.html"
basic_path = str(os.path.dirname(__file__)) + "/"
js = "var q=document.documentElement.scrollTop=100"
my_json = {"全国": {}}
save_json_path = basic_path + "whether.json"
driver_path = basic_path + "../chromedriver.exe"
province_pinyin = {
    "湖南": "changsha",
    "湖北": "wuhan",
    "广东": "guangzhou",
    "广西": "nanning",
    "河南": "zhengzhou",
    "河北": "shijiazhuang",
    "山东": "jinan",
    "山西": "taiyuan",
    "江苏": "nanjing",
    "江西": "nanchang",
    "浙江": "hangzhou",
    "黑龙江": "haerbin",
    "新疆": "wulumuqi",
    "宁夏": "yinchuan",
    "辽宁": "shenyang",
    "青海": "xining",
    "陕西": "xian",
    "甘肃": "lanzhou",
    "云南": "kunming",
    "贵州": "guiyang",
    "西藏": "lasa",
    "四川": "chengdu",
    "北京": "beijing",
    "上海": "shanghai",
    "天津": "tianjin",
    "内蒙古": "huhehaote",
    "重庆": "chongqing",
    "海南": "haikou",
    "福建": "fuzhou",
    "吉林": "changchun",
    "安徽": "hefei",
    "香港": "xianggang",
    "澳门": "aomen",
    "台湾": "taibei",
}
mounth_list = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"]
year_list = ["2018", "2019", "2020", "2021", "2022"]
temp_json = {y: {m: [] for m in mounth_list} for y in year_list}


def get_whether():
    for k, v in province_pinyin.items():
        options = Options()
        service = Service(driver_path)
        options.add_argument('--disable-extensions')
        options.add_argument('--blink-settings=imagesEnabled=false')
        driver = webdriver.Chrome(options=options, service=service)
        my_json[k] = {}
        for y in year_list:
            my_json[k][y] = {}
            for m in mounth_list:
                url = basic_url.format(p=v, y=y, m=m)
                try:
                    try:
                        driver.get(url)
                        t = WebDriverWait(driver, 10).until(expected_conditions.presence_of_element_located(
                            (By.XPATH, xpath))).text
                    except:
                        time.sleep(10.1)
                        driver.get(url)
                        t = WebDriverWait(driver, 10).until(expected_conditions.presence_of_element_located(
                            (By.XPATH, xpath))).text
                    t = int(t.replace(tem, ""))
                    my_json[k][y][m] = t
                    temp_json[y][m].append(t)
                    time.sleep(random.random() * 10)
                except:
                    print(url)
                    my_json[k][y][m] = url
        driver.quit()
    save_json(save_json_path, my_json)
    for y, v in temp_json.items():
        for m in v.keys():
            temp_json[y][m] = sum(temp_json[y][m]) / len(temp_json[y][m])
    my_json["全国"] = temp_json
    save_json(save_json_path, my_json)


get_whether()
